	
*============================================ MAIN FIGURES =================================================================*

*-----------------------------------
* FIGURE 1
*-----------------------------------

		import excel using "$repfinaldata\emdat_public_all.xlsx", cellrange(A7:AX14602) clear firstrow allstring
		tab DisasterSubgroup
		tab DisasterType
		destring(Seq), replace
		destring(Year), replace
		destring(TotalDamagesAdjusted000US), replace 
		destring(TotalDeaths), replace 
		tab DisasterType
		tab DisasterSubtype if DisasterType=="Storm"
		keep if DisasterType=="Flood" | DisasterType=="Storm"

		* gen variables
		bys Year: egen damages_thou = sum(TotalDamagesAdjusted000US)
		bys Year: egen deaths = sum(TotalDeaths)
		bys Year: gen total = _N

		* create key graphs: worldwide 
		gen damages_mill = damages_thou / 1000
		gen damages_bill = damages_mill / 1000
		drop if Year==2021
		drop if Year < 1920
		format damages_bill %9.0gc
		graph twoway ///
			(line total Year, yaxis(1) lwidth(thick)) ///
			(line damages_bill Year, yaxis(2) lwidth(thick) lpattern(dash)), ///
			xlab(1920(20)2020, labsize(medlarge)) ylab(, labsize(medlarge) axis(1)) ylab(, labsize(medlarge) axis(2)) ///
			title("") ///
			xtitle("") ytitle("Number of Floods" " ", axis(1) size(medlarge)) ytitle("Damages (Billions 2000USD)", axis(2) size(medlarge)) ///
			graphregion(color(white)) ///
			legend(order(1 "Number of Floods (left)   " 2 "Flood Damage (right)") region(color(white)) size(medlarge)) ///
			note("Source: EM-DAT International Disaster Database", size(med))
		graph export "$repoutput/floods_century.png", as(png) replace 


			
**--------------------------------------------------------------------------------------------------------------------
** FIGURE 2
**--------------------------------------------------------------------------------------------------------------------

	use "$repfinaldata/matchedloans_finalsample.dta", clear 

	// create the LTV and interest rate categories 
	numlist "0 .5(.01)1.1"
	local ltv_group `r(numlist)'
		
	numlist "0 .02(.002).06"
	local rate_group `r(numlist)'
	di "rate_group'"
	
	egen ltv_cat = cut(OriginalLTV), at(`ltv_group') nolabel
	egen rate_cat = cut(OriginalInterestRate), at (`rate_group') nolabel
	
	replace ltv_cat = .49 if ltv_cat == 0 // censoring below 
	replace rate_cat = .02-.002 if rate_cat == 0  // rate censoring below 
	
	graph twoway ///
		(hist ltv_cat if I_SHFA==1, color("$pgp_yellow") discrete fraction ) ///
		(hist ltv_cat if I_SHFA==0, ///
		fcolor(none) lcolor(black) discrete fraction ),  ///
		legend(order(1 "In Flood Zone" 2 "Outside Flood Zone" ) region(lcolor(white))) ///
		graphregion(fcolor(white) lcolor(white) margin(0)) ///
		xlab(0.5(.05)1.1, valuelabel) ///
		xtitle("") xtitle("Loan-to-Value Ratio", size(large))
		graph export "$repoutput/hist_ltv_shfa_cat.png", as(png) replace 
	
	graph twoway ///
		(hist rate_cat if I_SHFA==1, color("$pgp_yellow") discrete fraction ) ///
		(hist rate_cat if I_SHFA==0, ///
		fcolor(none) lcolor(black) discrete fraction ),  ///
		legend(order(1 "In Flood Zone" 2 "Outside Flood Zone" ) region(lcolor(white))) ///
		graphregion(fcolor(white) lcolor(white) margin(0)) ///
		xlab(.02(.005).06, valuelabel) ///
		xtitle("") xtitle("Interest Rate", size(large))
		graph export "$repoutput/hist_rate_shfa_cat.png", as(png) replace 

 


	
**--------------------------------------------------------------------------------------------------------------------
** FIGURE 3 
**-----------------------------------------------------------------------------------------------------------------
	use "$repfinaldata/matchedloans_finalsample.dta", clear 
	* generate replacement cost categories 
	summ r2, d
	egen repval_cat = cut(r2), at(0 75000 100000 125000 150000 175000 200000 225000 250000 275000 300000 325000 350000 375000 400000 425000 450000 475000 500000 `r(max)') label
	tabstat r2, by(repval_cat) stat(min max mean)
	format r2_thou %9.4gc
	decode(repval_cat), gen(repval)
	replace repval = subinstr(repval, "-", "", .)
	destring(repval), replace
	drop if mi(repval)

	
	reghdfe OriginalLTV b1.repval_cat##I_SHFA_final $restricted if repval_cat>0, cl(FIPS) absorb(fegroup)
	parmest,format(estimate min95 max95 %8.4f p %8.4e) saving($repoutput/parmest_cap_repval_ltv_restricted, replace) 
	reghdfe OriginalInterestRate b1.repval_cat##I_SHFA_final $restricted if repval_cat>0, cl(FIPS) absorb(fegroup)
	parmest,format(estimate min95 max95 %8.4f p %8.4e) saving($repoutput/parmest_cap_repval_rate_restricted, replace) 
	reghdfe I_del b1.repval_cat##I_SHFA_final $restricted if repval_cat>0, cl(FIPS) absorb(fegroup)
	parmest,format(estimate min95 max95 %8.4f p %8.4e) saving($repoutput/parmest_cap_repval_30delinq_restricted, replace) 


	// MAKING THE GRAPHS(restricted controls)
	foreach var in ltv rate 30delinq   {

		use $repoutput/parmest_cap_repval_`var'_restricted, clear 
		
		split(parm), parse(#)
		gen group = "int" if strpos(parm2, "I_SHFA_final")>0
		replace group = "" if strpos(parm2, "0b")>0	
		keep if !mi(group)
		drop parm2 group 
		rename parm1 repval_cat 
		replace repval_cat = subinstr(repval_cat, ".repval_cat", "", .)
		rename repval_cat repval_cat_str

		destring(repval_cat_str), gen(repval_cat) force	
		replace repval_cat = 1 if repval_cat_str=="1b"
		gen repval = . 
		replace repval =  0 if repval_cat == 0
		replace repval =  75000 if repval_cat == 1
		replace repval =  100000 if repval_cat == 2
		replace repval =  125000 if repval_cat == 3
		replace repval =  150000 if repval_cat == 4
		replace repval =  175000 if repval_cat == 5
		replace repval =  200000 if repval_cat == 6
		replace repval =  225000 if repval_cat == 7
		replace repval =  250000 if repval_cat == 8
		replace repval =  275000 if repval_cat == 9
		replace repval =  300000 if repval_cat == 10
		replace repval =  325000 if repval_cat == 11
		replace repval =  350000 if repval_cat == 12
		replace repval =  375000 if repval_cat == 13
		replace repval =  400000 if repval_cat == 14
		replace repval =  425000 if repval_cat == 15
		replace repval =  450000 if repval_cat == 16
		replace repval =  475000 if repval_cat == 17
		replace repval =  500000 if repval_cat == 18

		// base level 
		gen estimate2 = estimate * 100 * 100 
		gen min95_2 = min95 * 100 * 100 
		gen max95_2 = max95 * 100 * 100 
		replace estimate2 = 0 if repval_cat_str=="1b"
		replace min95_2 = 0 if repval_cat_str=="1b"
		replace max95_2 = 0 if repval_cat_str=="1b"
		
		gen repval2 = repval/1000
		format repval %15.2gc		
		
		local ynote_ltv "-500(150)100"
		
		graph twoway ///
			(scatter estimate2 repval2, color("$pgp_blue")) ///
			(rcap min95_2 max95_2 repval2, color("$pgp_blue")), ///
			yline(0, lcolor(black)) ///
			xline(250, lcolor(cranberry)) ///
			xlab(, labsize(large)) ///
			ylab(`ynote_`var'', labsize(large)) ///
			graphregion(fcolor(white) lcolor(white) ) ///
			legend(order(1 "Estimate" 2 "95% Confidence Interval") region(lcolor(white)) size(large)) ///
			xtitle("Replacement Cost (Thousands)", size(large)) ///
			ytitle("Coefficient Estimates {&phi}{sub:k}" "(Basis Points)", size(large)) title("")
			
		graph export "$repoutput/repval_rkd_coeff_`var'_bp_restricted.png", as(png) replace 
	}
	

	


**-------------------------------------------------------------------------------------
** FIGURE 4 
**-------------------------------------------------------------------------------------

	use $repfinaldata/eventstudy_parcel_FL13, clear 	
	
	*Panel A 
	
	est drop _all 
	reghdfe OriginalLTV b9.event_time_y2  if fz_grew3==1 & high_risk==0 , absorb(county year) cl(county) 
	eststo t0 
	reghdfe OriginalLTV b9.event_time_y2  if fz_grew3==1 & high_risk==1, absorb(county year) cl(county) 
	eststo t1 
	
	coefplot ///
		(t0, label("Low Risk") mcolor("$pgp_blue") msymbol(T) ciopts(lcolor("$pgp_blue") recast(rcap))) ///
		(t1, label("High Risk") mcolor("$pgp_red") msymbol(O) ciopts(lcolor("$pgp_red") recast(rcap))) ///
		, ///
		vertical base ///
			keep(7.event_time_y2 8.event_time_y2 9.event_time_y2 10.event_time_y2 11.event_time_y2 12.event_time_y2 13.event_time_y2) ///
			rename(	7.event_time_y2="-3" ///
				8.event_time_y2="-2" ///
				9.event_time_y2="-1" ///
				10.event_time_y2 = "0" ///
				11.event_time_y2 = "1" ///
				12.event_time_y2 = "2" ///
				13.event_time_y2 = "3") ///
			yline(0)  msymbol(O) ///
			ylab(-.1(.05).05, labsize(large)) ///
			xlab(,labsize(large)) ///
		xtitle("Years Since Updated Map Released", size(large)) 
		
	graph export "$repoutput/eLTV_highrisk.png", as(png) replace 
	
	*Panel B 	
		
	est drop _all 
	reghdfe OriginalLTV b9.event_time_y2 if fz_grew3==1 & high_risk==1 & r2 > 200000 & r2 < 250000 , absorb(county year) cl(county) 
	eststo t0 
	reghdfe OriginalLTV b9.event_time_y2 if fz_grew3==1 & high_risk==1  & r2>=250000 & r2<=350000 , absorb(county year) cl(county) 
	eststo t1 
	reghdfe OriginalLTV b9.event_time_y2 if fz_grew3==1 & high_risk==1   & r2>350000, absorb(county year) cl(county) 
	eststo t2 
	
	coefplot ///
		(t0, label("200-250K") mcolor("$pgp_blue") msymbol(T) ciopts(lcolor("$pgp_blue") recast(rcap))) ///
		(t1, label("250K-350K") mcolor("$pgp_red") msymbol(O) ciopts(lcolor("$pgp_red") recast(rcap))) ///
		(t2, label(">350K") mcolor(black) msymbol(O) ciopts(lcolor(black) recast(rcap))) ///
		, ///
		vertical base ///
			keep(7.event_time_y2 8.event_time_y2 9.event_time_y2 10.event_time_y2 11.event_time_y2 12.event_time_y2 13.event_time_y2) ///
			rename(	7.event_time_y2="-3" ///
				8.event_time_y2="-2" ///
				9.event_time_y2="-1" ///
				10.event_time_y2 = "0" ///
				11.event_time_y2 = "1" ///
				12.event_time_y2 = "2" ///
				13.event_time_y2 = "3") ///
			yline(0)  msymbol(O) ///
			ylab(-.1(.05).05, labsize(large)) ///
			xlab(,labsize(large)) ///
		xtitle("Years Since Updated Map Released", size(large)) 
	
	graph export "$repoutput/eLTV_highrisk_capbinds.png", as(png) replace 
		
	*Panel C 	

	est drop _all 
	reghdfe OriginalLTV b9.event_time_y2 if fz_grew3==1 & high_risk==1 & r2 > 200000 & r2 < 240000 , absorb(county year) cl(county) 
	eststo t0 
	reghdfe OriginalLTV b9.event_time_y2 if fz_grew3==1 & high_risk==1  & r2>=260000 & r2<=350000 , absorb(county year) cl(county) 
	eststo t1 
	reghdfe OriginalLTV b9.event_time_y2 if fz_grew3==1 & high_risk==1   & r2>350000, absorb(county year) cl(county) 
	eststo t2 
	
	coefplot ///
		(t0, label("200-240K") mcolor("$pgp_blue") msymbol(T) ciopts(lcolor("$pgp_blue") recast(rcap))) ///
		(t1, label("260K-350K") mcolor("$pgp_red") msymbol(O) ciopts(lcolor("$pgp_red") recast(rcap))) ///
		(t2, label(">350K") mcolor(black) msymbol(O) ciopts(lcolor(black) recast(rcap))) ///
		, ///
		vertical base ///
			keep(7.event_time_y2 8.event_time_y2 9.event_time_y2 10.event_time_y2 11.event_time_y2 12.event_time_y2 13.event_time_y2) ///
			rename(	7.event_time_y2="-3" ///
				8.event_time_y2="-2" ///
				9.event_time_y2="-1" ///
				10.event_time_y2 = "0" ///
				11.event_time_y2 = "1" ///
				12.event_time_y2 = "2" ///
				13.event_time_y2 = "3") ///
			yline(0)  msymbol(O) ///
			ylab(-.1(.05).05, labsize(large)) ///
			xlab(,labsize(large)) ///
		xtitle("Years Since Updated Map Released", size(large)) 
		
		
	graph export "$repoutput/eLTV_highrisk_capbinds_measurementerror.png", as(png) replace 
	
	* Panel D
	est drop _all 
	reghdfe OriginalLTV b9.event_time_y2 if fz_grew3==1 & very_high_risk==1 & r2 > 200000 & r2 < 240000 , absorb(county year) cl(county) 
	eststo t0 
	reghdfe OriginalLTV b9.event_time_y2 if fz_grew3==1 & very_high_risk==1  & r2>=260000 & r2<=350000 , absorb(county year) cl(county) 
	eststo t1 
	reghdfe OriginalLTV b9.event_time_y2 if fz_grew3==1 & very_high_risk==1   & r2>350000, absorb(county year) cl(county) 
	eststo t2 
	
	
	
	coefplot ///
		(t0, label("200-240K") mcolor("$pgp_blue") msymbol(T) ciopts(lcolor("$pgp_blue") recast(rcap))) ///
		(t1, label("260K-350K") mcolor("$pgp_red") msymbol(O) ciopts(lcolor("$pgp_red") recast(rcap))) ///
		(t2, label(">350K") mcolor(black) msymbol(O) ciopts(lcolor(black) recast(rcap))) ///
		, ///
		vertical base ///
			keep(7.event_time_y2 8.event_time_y2 9.event_time_y2 10.event_time_y2 11.event_time_y2 12.event_time_y2 13.event_time_y2) ///
			rename(	7.event_time_y2="-3" ///
				8.event_time_y2="-2" ///
				9.event_time_y2="-1" ///
				10.event_time_y2 = "0" ///
				11.event_time_y2 = "1" ///
				12.event_time_y2 = "2" ///
				13.event_time_y2 = "3") ///
			yline(0)  msymbol(O) ///
			ylab(-.1(.05).05, labsize(large)) ///
			xlab(,labsize(large)) ///
		xtitle("Years Since Updated Map Released", size(large)) 
	
	graph export "$repoutput/eLTV_vhighrisk_capbinds_measurementerror.png", as(png) replace 

